package com.huisou.web;

import com.huisou.dao.HotwordDao;
import com.huisou.domain.Hotwords;
import com.huisou.utils.UniqueStringList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SearchAction {
	
	private static final Logger	log	= LoggerFactory.getLogger(SearchAction.class);
	
	@RequestMapping(value = "/index.htm")
	public String index() {
		
		return "index";
	}
	
	@Resource
	private HotwordDao	hd;
	
	@RequestMapping(value = "/hotwords.htm")
	@ResponseBody
	public String hotWords(Model mod) {
		log.info("显示随机的几个热门关键字");
		Hotwords hotwords = null;
		List<Hotwords> list = hd.getAll();
		ObjectMapper om = new ObjectMapper();
		List<String> words = new ArrayList<String>();
		if (list.size() > 0) {
			hotwords = list.get(0);
		}
		if (null != hotwords) {
			String items = hotwords.getKeywords();
			if (StringUtils.isNotBlank(items)) {
				String[] dd = items.split(",");
				for (int i = 0; i < dd.length; i++) {
					String keyword = dd[i].trim();
					if (StringUtils.isNotBlank(keyword)) {
						words.add(keyword);
					}
				}
			}
		}
		try {
			return om.writeValueAsString(words);
		}
		catch (IOException e) {
			log.error("获取随机热门关键词失败:" + e.getMessage());
			return "";
		}
	}
}
